import torch


def corners2centers(corners):
    if corners.shape[-1] != 4:
        raise IOError("corners the last dim is 4")
    x1 = corners[:, 0]
    y1 = corners[:, 1]
    x2 = corners[:, 2]
    y2 = corners[:, 3]

    w = x2 - x1
    h = y2 - y1
    cx = x1 + w / 2
    cy = y1 + h / 2

    return torch.stack([cx, cy, w, h], dim=-1)
